package com.railway.dao.impl;

import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import com.railway.bean.LoseCreditUser;
import com.railway.dao.LoseCreditDao;
import com.railway.util.DBUtil;

public class LoseCreditDaoImpl implements LoseCreditDao {
	JdbcTemplate jt = new JdbcTemplate(DBUtil.getDataSource());
	@Override
	public List<LoseCreditUser> selectUser(String user_name, String user_id_num) {
		String sql = "select * from losecredit where user_name = ? and user_id_num = ?";
		List<LoseCreditUser> list = jt.query(sql, new BeanPropertyRowMapper<LoseCreditUser>(LoseCreditUser.class),user_name,user_id_num);
		if(list.size() == 0) {
			return null;
		}else {
			return list;
		}
	}
	@Override
	public int selectAllUser() {
		String sql = "select count(1) from losecredit";
		return jt.queryForObject(sql, Integer.class);
	}

	@Override
	public int insertLoseCredit(LoseCreditUser l) {
		String sql = "insert into losecredit(user_name,user_id_num) values(?,?)";
		return jt.update(sql,l.getUser_name(),l.getUser_id_num());
	}

	@Override
	public List<LoseCreditUser> selectUserAll(int pageNum,int pageSize) {
		String sql = "select * from losecredit where is_quit = 1 limit ?,?";
		List<LoseCreditUser> list = jt.query(sql, new BeanPropertyRowMapper<LoseCreditUser>(LoseCreditUser.class),(pageNum-1)*pageSize,pageSize);
		return list;
	}
	@Override
	public List<LoseCreditUser> selectUserAllByAdmin(int pageNum, int pageSize) {
		String sql = "select * from losecredit limit ?,?";
		List<LoseCreditUser> list = jt.query(sql, new BeanPropertyRowMapper<LoseCreditUser>(LoseCreditUser.class),(pageNum-1)*pageSize,pageSize);
		return list;
	}


	@Override
	public int delAbsoluteLcu(int loseCreditId) {
		String sql = "delete from losecredit where loseCredit_id = ?";
		return jt.update(sql,loseCreditId);
	}
	
	@Override
	public int deleteLoseCreditUser(int loseCredit_id) {
		String sql = "update losecredit set is_quit = 0 where loseCredit_id = ?";
		return jt.update(sql,loseCredit_id);
	}
	
	@Override
	public List<LoseCreditUser> selectLcuLimit(String key,int pageNum,int pageSize) {
		String sql = "select * from losecredit where user_name = ? or user_id_num = ? limit ?,?";
		
		return jt.query(sql,new BeanPropertyRowMapper<LoseCreditUser>(LoseCreditUser.class),key,key,(pageNum-1)*pageSize,pageSize);
	}
	
	@Override
	public int selectCountLcu(String key) {
		String sql = "select count(1) from losecredit where user_name = ? or user_id_num = ? ";
		return jt.queryForObject(sql, Integer.class,key,key);
	}
}
